/**
 * 
 */
define([],function(){
	webUtils.nameSpace("wiseMen.platform.dataDicSelector");

	wiseMen.platform.dataDicSelector=function(params){
		params = $.extend({
	        scope:this,
	        dataDicCode:"",
	        hasSelectedDataItem:[],
	        isSingle:false,
	        onSelect:function(rows){}
	    }, params);
		var hasSelectedGrid;
		var dataDicItemTree;
		var dialog =webUtils.dialog({
			title: '选择数据字典',
			onremove: function () {
                if (params.onClose) {
                    params.onClose.call(null);
                }
            },
            buttons: [{
            	label: '关闭',
                action: function (dialog) {
                    dialog.close();
                }
            },{
            	label: '确定',
            	icon: 'glyphicon glyphicon-check',
                cssClass: 'btn-primary',
                action: function (dialog) {
                    var rows=hasSelectedGrid.bootstrapTable("getData");
                    params.onSelect.call(params.scope,rows);
                    dialog.close();
                }
            }],
            onshown:function(dialog){
            	var body = dialog.getModalBody();
        		body.append(wiseMen.platform.dataDicSelector.temp);
        		var mainContent = body.find(".mainContent");
        		
        		dataDicItemTree = new wiseMen.platform.dataDicSelector.dataDicItemTree({
        			dataDicCode:params.dataDicCode,
        	        target: mainContent.find("._dataDicItemTree"),
        	        onClick: function (e, treeId, treeNode, clickFlag) {
        	            if (clickFlag != 0) {
        	                onSelectDataDicItem(treeNode);
        	            }
        	        }
        	    });
        		
        		hasSelectedGrid=new wiseMen.platform.dataDicSelector.hasSelectedBox({
        	        target: mainContent.find("._hasSelectedBox"),
        	        hasSelectedDataItem:params.hasSelectedDataItem
        	    });
            }
		});
		
		
		
		function onSelectDataDicItem(treeNode) {
	        if(treeNode.id=='-1'){return;}
	        var row= hasSelectedGrid.bootstrapTable(
					"getRowByUniqueId", treeNode.code);
	        if(!row){
	        	if(params.isSingle){
	            	hasSelectedGrid.bootstrapTable('removeAll');
	            }
	        	
	            hasSelectedGrid.bootstrapTable("append", treeNode);
	        }
	    }
	}
	
	wiseMen.platform.dataDicSelector.dataDicItemTree=function(params){
		params = $.extend({
	    }, params);
		var dataDicItemTree = $.fn.zTree.init(params.target, {
            async: {
                enable: true,
                url: ctx + "/dataDicAction/queryDataDicItemTreeNodes.do",
                autoParam: ["id=pid"],
                otherParam: {dataDicCode:params.dataDicCode},
                dataFilter: function (treeId, parentNode, responseData) {
                    if (responseData) {
                        $.each(responseData, function (i, n) {
                            if (n.childrenSize > 0) {
                                n.isParent = true;
                            }
                        });
                    }
                    return responseData;
                }
            },
            data: {
                key: {
                    name: "text"
                }
            },
            callback: {
                onClick: params.onClick
            }
        }, [{
                text: "数据字典",
                id: "-1",
                isParent: true,
                open: true
            }]);
		dataDicItemTree.reAsyncChildNodes(dataDicItemTree.getNodesByParam("id", "-1")[0], "refresh");
	    return dataDicItemTree;
	}
	
	wiseMen.platform.dataDicSelector.hasSelectedBox=function(params){
		params = $.extend({
	        hasSelectedOrgs:[]
	    }, params);
	    var grid = params.target.bootstrapTable({
	        columns: [
	                {title: '名称', field: 'name', align: 'left',width:300},
	                {title: '编码', field: 'code', align: 'left',width:300}
	            ],
	        data:params.hasSelectedDataItem,
	        uniqueId:"code",
	        sidePagination : 'client',
			pagination : false,
			onClickRow : function(row) {
				grid.bootstrapTable("removeByUniqueId", row.code);
			}
	    });
	    return grid;
	}
	
	wiseMen.platform.dataDicSelector.temp='<div class="mainContent row">'
        + '<div title="字典列表" class="col-md-6"><div class="_dataDicItemTree ztree"></div></div>'
        + '<div title="已选列表" class="col-md-6" ><table class="_hasSelectedBox"></table></div>'
        + '</div>';
	
	return wiseMen.platform.dataDicSelector;
});